package actions;

import java.awt.event.ActionEvent;

import client.ClientApplication;
import client.ClientBindObject;
import client.ClientSocketConnection;
import client_gui.ClientWindow;
import client_tree.User;

public class BlockUserAction extends RLMAbstractAction {

	
	public BlockUserAction(ClientWindow owner) {
		setActionOwner(owner);
//		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0));
		putValue(SMALL_ICON, loadIcon("images/block.png"));
		putValue(NAME, "Block User");
		putValue(SHORT_DESCRIPTION, "Blokiranje korisnika");
	}
	
	
	public void actionPerformed(ActionEvent e) {

		ClientSocketConnection connection = getActionOwner().getApplicaton().retrieveSocketConnection();

		User user = getActionOwner().getSelectedUser();
		
		StringBuffer blockCommand = new StringBuffer();
		
		blockCommand.append("BLOCK ");
		blockCommand.append(getActionOwner().getApplicaton().getClientUser().getUsername());
		blockCommand.append(" ");
		blockCommand.append(user.getUsername());
		
		String blockCommandCompleted = blockCommand.toString();

		connection.setMessage(BLOCK_CMD, blockCommandCompleted);
		
		String response = connection.buffers[BLOCK_CMD].read();
		
		if (response.startsWith("100")) {
			getActionOwner().getApplicaton().getClientUser().addToBlockList(user.getUsername());
			getActionOwner().refreshTree();
			
			ClientBindObject cbo = ClientApplication.clientBinding.remove(user.getUsername());
			if (cbo != null) {
				if (cbo.getDialog() != null) {
					cbo.getDialog().setConnection(null);
					cbo.getDialog().dispose();
				}
				cbo.getConnection().quitConnection();
				cbo.setConnection(null);
			}

		}
	
	}
	
}
